import tracker from '../utils/tracker'
import onLoad from '../utils/onLoad'
function getSelector(element) {
    var selector;
    if (element.id) {
        selector = `#${element.id}`;
    } else if (element.className && typeof element.className === 'string') {
        selector = '.' + element.className.split(' ').filter(function (item) { return !!item }).join('.');
    } else {
        selector = element.nodeName.toLowerCase();
    }
    return selector;
}

export function injectBlank(){
  // 作为节点容器的选择器
  const wrapperSelectors = ['body', 'html', '#container', '.content'];
  let emptyPoints = 0 // 计数器
  function isWrapper(elements){
    const selector = getSelector(elements)
    if(wrapperSelectors.indexOf(selector) >= 0){
      emptyPoints++
    }
  }
 // dom加载完成再进行检测
  onLoad(function(){
     // x轴和y轴各取9个点，然后依次检测点是否为body或者html或者app容器
    // 如果数量高达16个那么就断定为白屏
    let xElements,yElements
    for(let i=1;i<=9;i++){
       xElements = document.elementsFromPoint(window.innerWidth * i / 10, window.innerHeight / 2)
       yElements = document.elementsFromPoint(window.innerWidth / 2, window.innerHeight * i / 10)
      isWrapper(xElements[0])
      isWrapper(yElements[0])
    }
    if(emptyPoints >= 16){
      // 取最中心点为选择器
      let centerElements = document.elementsFromPoint(window.innerWidth / 2,window.innerHeight / 2)
      tracker.send({
        kind:'stability',
        type:'blank',
        emptyPoints: "" + emptyPoints, // 阿里云上传接口不允许有数字
        screen:window.screen.width + "x" + window.screen.height,
        viewPoint:window.innerWidth + "x" + window.innerHeight,
        selector:getSelector(centerElements[0])
      })
    }
  })
}